package server;

import java.io.Serializable;

import exception.BaseException;

/**
 * 代表服务器对客户调用的反馈
 * 包含: 正确时候返回的 方法值
 * 和异常返回的异常消息已经异常Code
 * @author teacher
 *
 */
public class Response 
  implements Serializable {
 
  private static final long serialVersionUID = -3767951101358054843L;

  private String sessionId;
  
  /** 方法的返回值 */
  private Object value;  
  
  /** 返回状态, 200 正常的, 501... */
  private int state = 200;
  
  private String message = "Success";

  public Response() {
  }
  /** 任意异常的方法返回 */
  public Response(String sid, Exception e) {
    state = 500;
    this.sessionId = sid;
    message = e.getMessage();
  }
  /** 正常的方法返回 */
  public Response(String sid, Object val) {
    state = 200;
    this.sessionId = sid;
    value = val;
  }
  /** 异常的方法返回 */
  public Response(String sid, BaseException e) {
    state = e.getCode();
    this.sessionId = sid;
    message = e.getMessage();
  }
  
  
  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public String getSessionId() {
    return sessionId;
  }

  public void setSessionId(String sessionId) {
    this.sessionId = sessionId;
  }

  public int getState() {
    return state;
  }

  public void setState(int state) {
    this.state = state;
  }

  public Object getValue() {
    return value;
  }

  public void setValue(Object value) {
    this.value = value;
  }
  
  public boolean isSuccess(){
    return state==200;
  }
  
  @Override
  public String toString() {
    return "SessionId:" + sessionId + 
      ", state:"+ state + ", value:" + value;
  }
}
